package jwcb.javapjframework.ms_template.service.service;

import com.fasterxml.jackson.core.type.TypeReference;
import jwcb.javapjframework.common.redis.RedisService.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class RedisListTestService {

    @Autowired
    private RedisService redisService;

    private static final String TEST_KEY = "test:list:operations";

    public String testRedisListOperations() {
        StringBuilder result = new StringBuilder();
        result.append("Redis List 操作测试开始...\n\n");

        try {
            // 清理旧数据
            redisService.removeForAllList(TEST_KEY);

            // 1. 初始化列表测试
            List<String> initList = Arrays.asList("A", "B", "C");
            long initSize = redisService.setCacheList(TEST_KEY, initList);
            result.append("测试1: 初始化列表\n")
                    .append("预期长度: 3, 实际长度: ").append(initSize).append("\n")
                    .append("结果: ").append(initSize == 3 ? "成功" : "失败").append("\n\n");

            // 2. 头插法测试
            redisService.leftPushForList(TEST_KEY, "HEAD");
            long afterLeftPush = redisService.getCacheListSize(TEST_KEY);
            List<String> listAfterLeftPush = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试2: 头部插入元素\n")
                    .append("预期长度: 4, 实际长度: ").append(afterLeftPush).append("\n")
                    .append("预期首元素: HEAD, 实际首元素: ").append(listAfterLeftPush.get(0)).append("\n")
                    .append("结果: ").append(afterLeftPush == 4 && "HEAD".equals(listAfterLeftPush.get(0)) ? "成功" : "失败").append("\n\n");

            // 3. 尾插法测试
            redisService.rightPushForList(TEST_KEY, "TAIL");
            long afterRightPush = redisService.getCacheListSize(TEST_KEY);
            List<String> listAfterRightPush = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试3: 尾部插入元素\n")
                    .append("预期长度: 5, 实际长度: ").append(afterRightPush).append("\n")
                    .append("预期尾元素: TAIL, 实际尾元素: ").append(listAfterRightPush.get(listAfterRightPush.size()-1)).append("\n")
                    .append("结果: ").append(afterRightPush == 5 && "TAIL".equals(listAfterRightPush.get(4)) ? "成功" : "失败").append("\n\n");

            // 4. 头删测试
            redisService.leftPopForList(TEST_KEY);
            long afterLeftPop = redisService.getCacheListSize(TEST_KEY);
            List<String> listAfterLeftPop = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试4: 头部删除元素\n")
                    .append("预期长度: 4, 实际长度: ").append(afterLeftPop).append("\n")
                    .append("预期首元素: A, 实际首元素: ").append(listAfterLeftPop.get(0)).append("\n")
                    .append("结果: ").append(afterLeftPop == 4 && "A".equals(listAfterLeftPop.get(0)) ? "成功" : "失败").append("\n\n");

            // 5. 尾删测试
            redisService.rightPopForList(TEST_KEY);
            long afterRightPop = redisService.getCacheListSize(TEST_KEY);
            List<String> listAfterRightPop = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试5: 尾部删除元素\n")
                    .append("预期长度: 3, 实际长度: ").append(afterRightPop).append("\n")
                    .append("预期尾元素: C, 实际尾元素: ").append(listAfterRightPop.get(listAfterRightPop.size()-1)).append("\n")
                    .append("结果: ").append(afterRightPop == 3 && "C".equals(listAfterRightPop.get(2)) ? "成功" : "失败").append("\n\n");

            // 6. 修改元素测试
            redisService.setElementAtIndex(TEST_KEY, 1, "X");
            List<String> listAfterModify = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试6: 修改索引1的元素\n")
                    .append("预期: [A, X, C], 实际: ").append(listAfterModify).append("\n")
                    .append("结果: ").append(listAfterModify.equals(Arrays.asList("A", "X", "C")) ? "成功" : "失败").append("\n\n");

            // 7. 移除单个元素测试
            redisService.rightPushForList(TEST_KEY, "X");  // 添加重复元素 [A, X, C, X]
            redisService.removeForList(TEST_KEY, "X");
            List<String> listAfterRemoveOne = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试7: 移除第一个匹配元素\n")
                    .append("预期: [A, C, X], 实际: ").append(listAfterRemoveOne).append("\n")
                    .append("结果: ").append(listAfterRemoveOne.equals(Arrays.asList("A", "C", "X")) ? "成功" : "失败").append("\n\n");

            // 8. 移除所有匹配元素测试
            redisService.removeAllForList(TEST_KEY, "X");
            List<String> listAfterRemoveAll = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试8: 移除所有匹配元素\n")
                    .append("预期: [A, C], 实际: ").append(listAfterRemoveAll).append("\n")
                    .append("结果: ").append(listAfterRemoveAll.equals(Arrays.asList("A", "C")) ? "成功" : "失败").append("\n\n");


            redisService.removeForAllList(TEST_KEY);
            // 9. 范围查询测试
            redisService.setCacheList(TEST_KEY, Arrays.asList("1", "2", "3", "4", "5"));
            List<String> subList = redisService.getCacheListByRange(TEST_KEY, 1, 3, new TypeReference<List<String>>() {});
            result.append("测试9: 范围查询(索引1-3)\n")
                    .append("预期: [2, 3, 4], 实际: ").append(subList).append("\n")
                    .append("结果: ").append(subList.equals(Arrays.asList("2", "3", "4")) ? "成功" : "失败").append("\n\n");

            // 10. 清空列表测试
            redisService.removeForAllList(TEST_KEY);
            long finalSize = redisService.getCacheListSize(TEST_KEY);
            List<String> finalList = redisService.getCacheList(TEST_KEY, String.class);
            result.append("测试10: 清空列表\n")
                    .append("预期长度: 0, 实际长度: ").append(finalSize).append("\n")
                    .append("预期列表: [], 实际列表: ").append(finalList).append("\n")
                    .append("结果: ").append(finalSize == 0 && finalList.isEmpty() ? "成功" : "失败").append("\n\n");

            result.append("✅ 所有测试完成!");

        } catch (Exception e) {
            result.append("\n❌ 测试异常: ").append(e.getMessage());
            e.printStackTrace();
        } finally {
            // 清理测试数据
            redisService.removeForAllList(TEST_KEY);
        }

        return result.toString();
    }
}